package com.sgb.kjwl.view.ui.activity;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.viewpager.widget.ViewPager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPhotoViewBinding;
import com.sgb.kjwl.view.ui.adapter.PhotoPagerAdapter;

import java.util.List;

public class PhotoViewActivity extends AppActivity {
    public static final String KEY_PHOTO_URLS = "url";
    public static final String KEY_PHOTO_POSITION = "position";
    ActivityPhotoViewBinding binding;
    private int size;
    private boolean gonepreview;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        List<String> urls = (List<String>) getIntent().getSerializableExtra(PhotoViewActivity.KEY_PHOTO_URLS);
        size = urls.size();
        binding.viewpager.setVisibility(View.VISIBLE);
        binding.aliVideo.setVisibility(View.GONE);
        int position = getIntent().getIntExtra(KEY_PHOTO_POSITION, 0);
        binding.pageView.setText((position + 1) + "/" + size);
        PhotoPagerAdapter photoPagerAdapter = new PhotoPagerAdapter(PhotoViewActivity.this, urls);
        binding.viewpager.setAdapter(photoPagerAdapter);
        binding.viewpager.setCurrentItem(position);
        binding.pageView.setVisibility(getIntent().getBooleanExtra("gonepreview",false)?View.GONE:View.VISIBLE);
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.pageView.setText((position + 1) + "/" + size);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PhotoViewActivity.this, R.layout.activity_photo_view);
        boolean isGoneBack = getIntent().getBooleanExtra("goneBack", false);
        if (!isGoneBack){
            binding.tvActionBarIntroduction.setVisibility(View.VISIBLE);
        }else {
            binding.tvActionBarIntroduction.setVisibility(View.GONE);
        }
        binding.actionBarImageLeft.setOnClickListener(v -> finish());
    }

}
